package proxy;

/**
 * 代理模式:Proxy Pattern
 * 
 * @version 2009-6-27
 * @author Winty(wintys@gmail.com)
 */
public class ProxyTest {
	public static void main(String[] args) {
		Subject subject;
		subject = new ProxySubject();
		subject.request();
	}
}

/**
 * 抽象主题角色
 */
abstract class Subject {
	public abstract void request();
}

/**
 * 真实主题角色
 */
class RealSubject extends Subject {
	@Override
	public void request() {
		System.out.println("do request.");
	}
}

/**
 * 代理主题角色
 */
class ProxySubject extends Subject {
	private RealSubject realSubject;

	private void preRequest() {
		System.out.println("do preRequest");
	}

	public void request() {
		preRequest();

		if (realSubject == null) {
			realSubject = new RealSubject();
		}
		realSubject.request();

		postRequest();
	}

	private void postRequest() {
		System.out.println("do postRequest");
	}
}
